package org.mfoyou.agent.task.service;

import org.mfoyou.agent.common.inf.IThirdPartInfo;
import org.mfoyou.agent.task.utils.XinGeSendType;
import org.mfoyou.agent.utils.common.HttpsUtils;
import org.mfoyou.agent.utils.common.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tencent.xinge.XingeApp;

/**
 * 微信 && 信鸽 服务类
 *
 * @author hzhang7
 */
@Service
public class WxService {

    private static Logger logger = Logger.getLogger(WxService.class);

    @Autowired
    RedisCacheManger redisCacheManger;

    @Autowired
    IThirdPartInfo iThirdPartInfo;
    @Value("#{settings['sys.debug']?:false}")

    private Boolean isDebug;

    /**
     * 微信 redis 缓存前缀
     */
    private static String WX_TOKEN_CACHE_PREX = "wx:token:";
    private static String WX_MODEL_PREX = "wx:push:model:";

    /**
     * 友达超人 - 安卓信鸽推送配置
     */
    private static Long ACCESS_ID_SUPER_ANDRIOD = 2100328562L;
    private static String ACCESS_SECRET_SUPER_ANDRIOD = "d7f6e96c0ca04ed3bf227e930295a94a";

    /**
     * 友达超人 - 苹果信鸽推送配置
     */
    private static Long ACCESS_ID_SUPER_IOS = 2200337985L;
    private static String ACCESS_SECRET_SUPER_IOS = "9ce7a450b0379a1987a2308ed549be62";

    /**
     * 友达速帮 - 安卓信鸽推送配置
     */
    private static Long ACCESS_ID_SU_BANG_ANDRIOD = 2100339213L;
    private static String ACCESS_SECRET_SU_BANG_ANDRIOD = "0ff72f034b1eb34ed4efa03a9e0db117";

    /**
     * 获取信鸽实例
     *
     * @param type
     * @param systemType 系统类型: 0 - 安卓; 1 - 苹果
     * @return
     */
    public XingeApp getXinGeApp(Integer type, Integer systemType) {
        logger.info(String.format("获取信鸽实例: type=[%s], systemType=[%s]", type, systemType));
        if (systemType == XinGeSendType.ANDRIOD.getValue()) {
            if (type.intValue() == 0) {
                logger.info(String.format("友达超人 - 安卓信鸽推送配置 access_id=[%s],access_secret=[%s]", ACCESS_ID_SUPER_ANDRIOD, ACCESS_SECRET_SUPER_ANDRIOD));
                return new XingeApp(ACCESS_ID_SUPER_ANDRIOD, ACCESS_SECRET_SUPER_ANDRIOD);
            }
            logger.info(String.format("友达速帮 - 安卓信鸽推送配置 access_id=[%s],access_secret=[%s]", ACCESS_ID_SU_BANG_ANDRIOD, ACCESS_SECRET_SU_BANG_ANDRIOD));
            return new XingeApp(ACCESS_ID_SU_BANG_ANDRIOD, ACCESS_SECRET_SU_BANG_ANDRIOD);
        } else if (systemType == XinGeSendType.IOS.getValue()) {
            logger.info(String.format("友达超人 - 苹果信鸽推送配置 access_id=[%s],access_secret=[%s]", ACCESS_ID_SUPER_IOS, ACCESS_SECRET_SUPER_IOS));
            return new XingeApp(ACCESS_ID_SUPER_IOS, ACCESS_SECRET_SUPER_IOS);
        }
        return null;
    }

    /**
     * 获取 token
     *
     * @param agentId
     * @param appid
     * @return
     */
    public String getTokenV1(Integer agentId, int appid) {
        String token = redisCacheManger.getValue(WX_TOKEN_CACHE_PREX + agentId + ":" + appid);
        if (token == null || token.equals("")) {
            String tokenUrl = null;
            tokenUrl = "http://api.udpt.cn/common/wxToken?token=111111asdwasdsss&appId="
                    + appid + "&agentId=" + agentId;
            String response = HttpsUtils.get(tokenUrl);
            if (response == null || response.equals("")) {
                return null;
            }
            com.alibaba.fastjson.JSONObject object = JSON.parseObject(response);
            token = object.getString("data");
            if (token != null) {
                redisCacheManger.setValue(WX_TOKEN_CACHE_PREX + agentId + ":" + appid, token, 200);
            }
        }
        return token;
    }

    public String getModelId(Integer agentId, Integer type) {
        String value = redisCacheManger.getValue(WX_MODEL_PREX + agentId + ":" + type);
        if (value == null || value.equals("")) {
            value = iThirdPartInfo.getModelId(agentId, type);
            if (value != null) {
                redisCacheManger.setValue(WX_MODEL_PREX + agentId + ":" + type, value, 60 * 60);
            }
        }
        return value;
    }

    public String getModelId(Integer areaAgentId, String types) {
        if (types.equals("1")) {
            return getModelId(areaAgentId, 5);
        } else if (types.equals("3")) {
            return getModelId(areaAgentId, 6);
        } else {
            return getModelId(areaAgentId, 7);
        }
    }
}
